package com.theanh.RssActivity.ui.model;

import com.theanh.RssActivity.MainController;
import com.theanh.RssActivity.R;
import com.theanh.RssActivity.Store;
import com.theanh.RssActivity.db.DbAdapter;
import com.theanh.RssActivity.entity.Category;
import com.theanh.RssActivity.util.DbUtils;

import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CategoryModel {
	private static final String TAG = "CategoryModel";
	ImageView icon;
	TextView name;
	TextView numberChannel;
	LinearLayout mainView;
	public Category category;

	public static CategoryModel createModel(View view) {
		CategoryModel model = new CategoryModel();

		model.icon = (ImageView) view.findViewById(R.id.categoryIconImageView);
		model.name = (TextView) view.findViewById(R.id.categoryNameTextView);
		model.name.setTypeface(Typeface.DEFAULT_BOLD);
		model.name.setTextColor(android.graphics.Color.WHITE);

		model.numberChannel = (TextView) view
				.findViewById(R.id.categoryNumberChannelTextView);
		model.numberChannel.setTextColor(android.graphics.Color.GRAY);

		model.mainView = (LinearLayout) view
				.findViewById(R.id.rowCategoryLinearLayout);
		// model.mainView.setOnClickListener(model.openLinkOnClick);
		model.mainView.setOnClickListener(model.openCategoryOnClick);
		model.mainView.setOnFocusChangeListener(model.focusChange);
		return model;
	}

	private OnClickListener openCategoryOnClick = new OnClickListener() {
		public void onClick(View view) {
			Log.i("CategoryModel", "openCategoryOnClick");
			if (Store.channelModelSelected != null) {
				Store.channelModelSelected = null;
				Store.hasChannelFocus = false;
			}
			MainController.showListChannelScreen(category);
		}
	};

	private OnFocusChangeListener focusChange = new OnFocusChangeListener() {
		public void onFocusChange(View v, boolean hasFocus) {
			if (hasFocus) {
				Store.hasCategoryFocus = true;
				Store.categoryModelSelected = CategoryModel.this;
				Store.categoryModelSelected.category = CategoryModel.this.category;

				Log.i(TAG, "Selected::"
						+ CategoryModel.this.category.toString());

			} else {
				Store.hasCategoryFocus = false;
			}
		}
	};

	public void bind(Category category) {
		this.category = category;
		name.setText(category.getName());

		// Open DB
		DbAdapter dbAdapter = DbUtils.createAndOpenDbAdapter();
		long number = dbAdapter.getNumberChannel(category);
		// Close DB
		DbUtils.closeDbAdapter(dbAdapter);
		// icon.setImageResource(R.id.categoryIconImageView);
		if (category.getIconID() != 0) {
			icon.setImageResource(category.getIconID());
		} else if (category.getIconID() == 0) {
			icon.setImageResource(R.drawable.categorytype_1);
		}
		String val = String.valueOf(number);
		CharSequence str = Store.context.getString(R.string.space);
		val += str.toString();
		str = Store.context.getString(R.string.channel);
		val += str.toString();
		numberChannel.setText(val);
	}

	public void updateNumberChannel(Category category) {
		// Open DB
		Log.i(TAG,"call updateNumberChannel");
		DbAdapter dbAdapter = null;
		long number=0;
		try{
			dbAdapter = DbUtils.createAndOpenDbAdapter();
			number = dbAdapter.getNumberChannel(category);
		}catch(Exception ex){
			//TODO
			Log.e(TAG,ex.toString());
		}finally{
			if(dbAdapter!= null){
//				Close DB
				DbUtils.closeDbAdapter(dbAdapter);
			}
		}
		String val = String.valueOf(number);
		val += " "+Store.context.getString(R.string.channel);
		numberChannel.setText(val);
	}

	public void updateName() {
		try {
			name.setText(category.getName());
			icon.setImageResource(category.getIconID());
		} catch (Exception ex) {
			// TODO show message dialog
			Log.e(TAG, ex.toString());
		}

	}
}
